This package implements various tests, visualizations, and metrics for use with environmental niche models (ENMs) and species distribution models (SDMs).
At present, ENMTools is downloadable from https://github.com/danlwarren/ENMTools. There are multiple ways to download it. The easiest is to use devtools and install from GitHub.
Run the following code from your R console:
install.packages("devtools")
library(devtools)
install_github("danlwarren/ENMTools")
library(ENMTools)
A zipped version of the package is available at https://github.com/danlwarren/ENMTools/archive/master.zip. To install from the zip file, download a copy of it to your system. Once it’s finished downloading, type the following (where PATH is the path to the zip file):
install.packages("devtools")
library(devtools)
install_local("PATH")
library(ENMTools)
First we’re going to load in some environmental data.
env.files <- list.files(path = "test/testdata/", pattern = "pc", full.names = TRUE)
env <- stack(env.files)
names(env) <- c("layer.1", "layer.2", "layer.3", "layer.4")
env <- setMinMax(env)
ENMTools is primarily designed to examine patterns of similarity and difference between ENMs for different species. In order to simplify interactions with the functions in ENMTools, you need to put your data for each of your species into an enmtools.species object. You can create and view an empty enmtools.species object just by typing:
ahli <- enmtools.species()
ahli
##
##
## Range raster not defined.
##
## Presence points not defined.
##
## Background points not defined.
##
## Species name not defined.
You can add data to this object manually:
names(ahli)
## [1] "range" "presence.points" "background.points"
## [4] "models" "species.name"
ahli$species.name <- "ahli"
ahli$presence.points <- read.csv("test/testdata/ahli.csv")[,3:4]
ahli$range <- background.raster.buffer(ahli$presence.points, 50000, mask = env)
ahli$background.points <- background.points.buffer(points = ahli$presence.points,
radius = 20000, n = 1000, mask = env[[1]])
ahli
##
##
## Range raster:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer.1
## values : 1, 1 (min, max)
##
##
##
## Presence points (first ten only):
##
## Longitude Latitude
## ---------- ---------
## -80.0106 21.8744
## -79.9086 21.8095
## -79.8065 21.7631
## -79.8251 21.8095
## -79.8807 21.8374
## -79.9550 21.8374
## -80.3446 22.0136
## -80.2983 21.9951
## -80.1776 21.9023
## -80.1591 21.9673
##
##
## Background points (first ten only):
##
## Longitude Latitude
## ---------- ---------
## -79.79559 21.71254
## -79.99559 21.73754
## -79.92059 21.75420
## -80.11226 21.97920
## -80.01226 21.79587
## -79.89559 22.07920
## -80.06226 22.06254
## -80.25392 22.11254
## -79.83726 22.00420
## -79.87059 21.97920
##
##
## Species name: ahli
Or you can add bits of it when the object is created:
allogus <- enmtools.species(species.name = "allogus",
presence.points = read.csv("test/testdata/allogus.csv")[,3:4])
allogus$range <- background.raster.buffer(allogus$presence.points, 50000, mask = env)
allogus$background.points <- background.points.buffer(points = allogus$presence.points,
radius = 20000, n = 1000, mask = env[[1]])
allogus
##
##
## Range raster:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer.1
## values : 1, 1 (min, max)
##
##
##
## Presence points (first ten only):
##
## Longitude Latitude
## ---------- ---------
## -79.2527 22.2109
## -78.7774 22.2241
## -78.6189 22.2373
## -78.1039 21.1809
## -78.0247 21.1809
## -77.9983 20.9301
## -77.9719 21.7091
## -77.9719 21.5507
## -77.9323 21.6167
## -77.9323 20.7320
##
##
## Background points (first ten only):
##
## Longitude Latitude
## ---------- ---------
## -78.05392 21.56254
## -77.94559 20.82920
## -78.07059 21.20420
## -74.67892 20.32920
## -74.97059 20.30420
## -74.25392 20.17087
## -76.85392 20.29587
## -74.87059 20.57920
## -76.66226 20.06254
## -77.88726 20.94587
##
##
## Species name: allogus
ENMTools contains functions to simplify the ENM construction process. Using enmtools.species objects and the correct modeling commands, we can build models very quickly. These commands are primarily wrappers to dismo model construction and projection functions, and at present are only available for GLM, Maxent, Domain, and Bioclim models. One of the nice bits about this setup is that it allows enmtools to automatically generate suitability maps, do model evaluation, and plot the marginal suitability of habitat for each variable separately.
GLMs usually require the user to supply a formula, an enmtools.species object, and some environmental data. If your formula is a strictly additive function of all of the environmental layers in env, though, enmtools.glm will build a formula automatically.
ahli.glm <- enmtools.glm(species = ahli, env = env, f = pres ~ layer.1 + layer.2 + layer.3 + layer.4, test.prop = 0.2)
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
allogus.glm <- enmtools.glm(allogus, env, f = pres ~ layer.1 + layer.2 + layer.3 + layer.4, test.prop = 0.2)
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
ahli.glm
##
##
## Formula: presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7fb549ca8380>
##
##
## Data table (top ten lines):
##
## Longitude Latitude layer.1 layer.2 layer.3 layer.4 presence
## --- ---------- --------- -------- -------- -------- -------- ---------
## 3 -79.8065 21.7631 2158 1870 983 253 1
## 4 -79.8251 21.8095 2207 1877 967 259 1
## 6 -79.9550 21.8374 2250 1766 919 235 1
## 7 -80.3446 22.0136 2201 1822 978 277 1
## 9 -80.1776 21.9023 2287 1722 992 266 1
## 10 -80.1591 21.9673 2984 965 1311 237 1
## 11 -80.1498 21.9858 3042 841 1371 221 1
## 12 -80.1220 21.9301 2898 1033 1231 242 1
## 13 -80.1776 21.9673 2914 1020 1256 237 1
## 14 -80.2148 21.9394 2329 1692 1018 269 1
##
##
## Model:
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1,
## 2)])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.5974 -0.1684 -0.1148 -0.0747 3.2673
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 51.232137 28.846937 1.776 0.0757 .
## layer.1 -0.014856 0.007428 -2.000 0.0455 *
## layer.2 -0.014062 0.007671 -1.833 0.0668 .
## layer.3 0.003375 0.006990 0.483 0.6292
## layer.4 -0.002495 0.025478 -0.098 0.9220
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 130.29 on 1011 degrees of freedom
## Residual deviance: 115.55 on 1007 degrees of freedom
## AIC: 125.55
##
## Number of Fisher Scoring iterations: 8
##
##
##
## Model fit (training data): class : ModelEvaluation
## n presences : 12
## n absences : 1000
## AUC : 0.7893333
## cor : 0.1163932
## max TPR+TNR at : -4.512834
##
##
## Proportion of data wittheld for model fitting: 0.2
##
## Model fit (test data): class : ModelEvaluation
## n presences : 4
## n absences : 1000
## AUC : 0.688
## cor : 0.03407783
## max TPR+TNR at : -5.24272
##
##
## Suitability:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer
## values : 1.076373e-06, 0.999996 (min, max)
Notice this produces the same formula as:
ahli.glm <- enmtools.glm(species = ahli, env = env, test.prop = 0.2)
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
ahli.glm
##
##
## Formula: presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7fb54a05b4d8>
##
##
## Data table (top ten lines):
##
## Longitude Latitude layer.1 layer.2 layer.3 layer.4 presence
## --- ---------- --------- -------- -------- -------- -------- ---------
## 1 -80.0106 21.8744 2765 1235 1174 252 1
## 2 -79.9086 21.8095 2289 1732 957 231 1
## 3 -79.8065 21.7631 2158 1870 983 253 1
## 4 -79.8251 21.8095 2207 1877 967 259 1
## 6 -79.9550 21.8374 2250 1766 919 235 1
## 7 -80.3446 22.0136 2201 1822 978 277 1
## 9 -80.1776 21.9023 2287 1722 992 266 1
## 10 -80.1591 21.9673 2984 965 1311 237 1
## 11 -80.1498 21.9858 3042 841 1371 221 1
## 12 -80.1220 21.9301 2898 1033 1231 242 1
##
##
## Model:
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1,
## 2)])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.6649 -0.1644 -0.1118 -0.0776 3.2028
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 39.360838 28.764972 1.368 0.171
## layer.1 -0.011116 0.007426 -1.497 0.134
## layer.2 -0.009588 0.007784 -1.232 0.218
## layer.3 0.005014 0.007107 0.706 0.480
## layer.4 -0.026267 0.027357 -0.960 0.337
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 130.29 on 1011 degrees of freedom
## Residual deviance: 114.75 on 1007 degrees of freedom
## AIC: 124.75
##
## Number of Fisher Scoring iterations: 8
##
##
##
## Model fit (training data): class : ModelEvaluation
## n presences : 12
## n absences : 1000
## AUC : 0.7947917
## cor : 0.1238435
## max TPR+TNR at : -4.284631
##
##
## Proportion of data wittheld for model fitting: 0.2
##
## Model fit (test data): class : ModelEvaluation
## n presences : 4
## n absences : 1000
## AUC : 0.631375
## cor : 0.02406816
## max TPR+TNR at : -4.912143
##
##
## Suitability:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer
## values : 2.220446e-16, 0.9999735 (min, max)
If you want a more complicated formula, though (e.g., with interactions or polynomial effects), you’ll need to supply that manually.
ahli.glm <- enmtools.glm(species = ahli, env = env, f = pres ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3, 2) + poly(layer.4, 2), test.prop = 0.2)
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
ahli.glm
##
##
## Formula: presence ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3,
## 2) + poly(layer.4, 2)
## <environment: 0x7fb5494e55b8>
##
##
## Data table (top ten lines):
##
## Longitude Latitude layer.1 layer.2 layer.3 layer.4 presence
## --- ---------- --------- -------- -------- -------- -------- ---------
## 3 -79.8065 21.7631 2158 1870 983 253 1
## 4 -79.8251 21.8095 2207 1877 967 259 1
## 6 -79.9550 21.8374 2250 1766 919 235 1
## 7 -80.3446 22.0136 2201 1822 978 277 1
## 8 -80.2983 21.9951 2214 1786 986 284 1
## 9 -80.1776 21.9023 2287 1722 992 266 1
## 10 -80.1591 21.9673 2984 965 1311 237 1
## 11 -80.1498 21.9858 3042 841 1371 221 1
## 12 -80.1220 21.9301 2898 1033 1231 242 1
## 14 -80.2148 21.9394 2329 1692 1018 269 1
##
##
## Model:
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1,
## 2)])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.6087 -0.1604 -0.1085 -0.0710 3.1199
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -5.1454 0.4965 -10.364 <2e-16 ***
## poly(layer.1, 2)1 -145.7854 62.6552 -2.327 0.0200 *
## poly(layer.1, 2)2 -7.3635 30.2030 -0.244 0.8074
## poly(layer.2, 2)1 -146.4279 71.1374 -2.058 0.0396 *
## poly(layer.2, 2)2 -1.7147 33.7056 -0.051 0.9594
## poly(layer.3, 2)1 8.7787 41.0069 0.214 0.8305
## poly(layer.3, 2)2 22.1048 17.1527 1.289 0.1975
## poly(layer.4, 2)1 13.5830 15.1544 0.896 0.3701
## poly(layer.4, 2)2 -18.0319 14.4122 -1.251 0.2109
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 130.29 on 1011 degrees of freedom
## Residual deviance: 114.07 on 1003 degrees of freedom
## AIC: 132.07
##
## Number of Fisher Scoring iterations: 8
##
##
##
## Model fit (training data): class : ModelEvaluation
## n presences : 12
## n absences : 1000
## AUC : 0.824
## cor : 0.1210654
## max TPR+TNR at : -4.859115
##
##
## Proportion of data wittheld for model fitting: 0.2
##
## Model fit (test data): class : ModelEvaluation
## n presences : 4
## n absences : 1000
## AUC : 0.65525
## cor : 0.03114882
## max TPR+TNR at : -4.773954
##
##
## Suitability:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer
## values : 2.220446e-16, 1 (min, max)
To check out the marginal response functions, you only need to type
ahli.glm$response.plots
## $layer.1
##
## $layer.2
##
## $layer.3
##
## $layer.4
You can also visualize your models and data in a 2D environment space using any pair of layers from your environment stack. These plots hold all non-plotted variables (layer.2 and layer.3 in this case) constant at their mean value across all presence points, then vary the plotted variables between the minimum and maximum values in env.
The suit.plot shows you suitability in environment space as a function of your two variables, with brighter colors representing variable combinations predicted to be more suitable. The points represent the occurrence points for your species in that environment space.
The colored raster of the background.plot shows you the density of background points in environment space, while the white points again represent your occurrence points in environment space.
visualize.enm(ahli.glm, env, layers = c("layer.2", "layer.4"))
## $suit.plot
##
## $background.plot
The procedure for building Bioclim, Domain, and Maxent models is similar to the procedure for GLMs, with the exception that you do not need to pass a formula to the model function. Note that running Maxent models requires a bit of extra setup; see dismo documentation for details.
ahli.dm <- enmtools.dm(ahli, env, test.prop = 0.2)
ahli.bc <- enmtools.bc(ahli, env, test.prop = 0.2)
ahli.mx <- enmtools.maxent(ahli, env, test.prop = 0.2)
ENMTools provides a number of metrics for ENMs and for similarities between ENMs. These include measures of niche breadth, based on Levins(1968). An important caveat when interpreting these metrics is that they are driven to some (variable) extent by the availability of different combinations of environmental variables. As such they are more accurately interpreted as a measurment of the smoothness of the geographic distribution of suitability scores than as an estimate of the breadth of the fundamental niche; an orgamism with narrow fundamental niche breadth that nonetheless encompasses a set of environmental conditions that is quite common will have a high breadth when measured using ENMs, while having a low breadth in environment space.
raster.breadth(ahli.glm)
## $B1
## [1] 0.8591662
##
## $B2
## [1] 0.1649415
ENMTools also provides metrics for measuring similarity between ENMs. These include Schoener’s D (Schoener 1968), I (Warren et al. 2008), and the Spearman rank correlation coefficient between two rasters. While D and I are commonly used in the ENM literature, they may tend to overestimate similarity between ENMs when many grid cells are of similar values (e.g., when two species prefer different habitat but the region contains a great deal of habitat that is unsuitable for both).
raster.overlap(ahli.glm, allogus.glm)
## $D
## [1] 0.3336511
##
## $I
## [1] 0.5976115
##
## $rank.cor
## [1] 0.4254904
A new feature of the R version of ENMTools is that you can now use these same metrics in the n-dimensional space of all combinations of environmental variables, instead of restricting your measures of model similarity to those sets of conditions that appear in the training region. This is done by repeatedly drawing Latin hypercube samples from the space of all possible combinations of environmental variables given the min and max of each variable within the training region. ENMTools continues to draw samples until subsequent iterations differ by less than a specified tolerance value. Lower tolerance values result in more precise estimates of overlap, but can take much longer to calculate.
env.overlap(ahli.glm, allogus.glm, env, tolerance = .001)
## $env.D
## [1] 0.2552654
##
## $env.I
## [1] 0.4534865
##
## $env.cor
## [1] 0.003465603
In this example, we will run a niche identity (also called equivalency) test, as in Warren et al. 2008. This test takes the presence points for a pair of species and randomly reassigns them to each species, then builds ENMs for these randomized occurrences. By doing this many times, we can estimate the probability distribution for ENM overlap between species under the null hypothesis that the two species’ occurrences in the environment are effectively a random draw from the same underlying distribution. Note that niche evolution is only one of many reasons why two species’ realized environmental distributions might cause departures from this null hypothesis. See Warren et al. 2014 for details.
To run an identity test, we need to decide what type of models we will build, how many replicates we will run, and (in the case of GLM) a model formula to use for empirical models and the Monte Carlo replicates. The resulting object contains the replicate models, p values, and plots of the results. Typically idenity tests are run with at least 99 replicates, but we are using a smaller number here for the sake of execution time.
NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the identity test are conducted with pseudoabsence points pooled for the two species being compared.
id.glm <- identity.test(species.1 = ahli, species.2 = allogus, env = env, type = "glm", nreps = 4)
id.glm
##
##
##
##
## Identity test ahli vs. allogus
##
## Identity test p-values:
## D I rank.cor env.D env.I env.cor
## 0.2 0.2 0.2 0.2 0.2 0.2
##
##
## Replicates:
##
##
## D I rank.cor env.D env.I env.cor
## ---------- ---------- ---------- ----------- ---------- ---------- -----------
## empirical 0.2370142 0.4876761 -0.4777383 0.0048948 0.0299632 -0.6073671
## rep 1 0.7886204 0.9671186 0.9715034 0.8133764 0.9652221 0.9714080
## rep 2 0.8332677 0.9796367 0.9726192 0.8101079 0.9671453 0.9395909
## rep 3 0.7882687 0.9653740 0.8132963 0.5972899 0.8680842 0.7003554
## rep 4 0.8538493 0.9796253 0.8274607 0.7943530 0.9645789 0.8914906
The background or similarity test compares the overlap seen between two species’ ENMs to the overlap expected by chance if one or both species was effectively choosing habitat at random from within their broad geographic range. The purpose of this test is to correct for the availability of habitat and ask whether the observed similarity between species or populations is significantly more (or less) than expected given the available set of environments in the regions in which they occur.
NOTE: In order for models to be comparable, both empirical and pseudoreplicate models for the background test are conducted with pseudoabsence points pooled for the two species being compared.
In Warren et al. 2008, we developed this test in the context of comparing one species’ actual occurrence to the random background occurrences of the other species. This is what we call an “asymmetric” test, and in our case we did the test in both directions with the idea that we might compare the results of A vs. B background to the results of B vs. A background. This may be informative in some cases, but many people have also found this asymmetry confusing (and indeed it is often difficult to interpret). For that reason, the background test here can be conducted against a null hypothesis that is generated from “asymmetric” (species.1 vs species.2 background) or “symmetric” (species.1 background vs. species.2 background) comparisons.
Here, for instance, is a Bioclim background test using the classical asymmetric approach:
bg.bc.asym <- background.test(species.1 = ahli, species.2 = allogus, env = env, type = "bc", nreps = 4, test.type = "asymmetric" )
bg.bc.asym
##
##
##
##
## Asymmetric background test ahli vs. allogus background
##
## background test p-values:
## D I rank.cor env.D env.I env.cor
## 0.2 0.2 0.2 0.2 0.2 0.2
##
##
## Replicates:
##
##
## D I rank.cor env.D env.I env.cor
## ---------- ---------- ---------- ---------- ---------- ---------- ----------
## empirical 0.1328502 0.3177390 0.0706201 0.0197694 0.1092342 0.0873851
## rep 1 0.1580439 0.3552792 0.1649878 0.0852103 0.2641877 0.2253952
## rep 2 0.1719212 0.3707013 0.2298201 0.0531358 0.1904490 0.1212580
## rep 3 0.1580122 0.3563211 0.1753061 0.0275076 0.1571274 0.1152632
## rep 4 0.1716781 0.3706148 0.1772590 0.0535762 0.1811610 0.1263951
And here is a Domain background test using the symmetric approach:
bg.dm.sym <- background.test(species.1 = ahli, species.2 = allogus, env = env, type = "dm", nreps = 4, test.type = "symmetric" )
bg.dm.sym
##
##
##
##
## Symmetric background test ahli background vs. allogus background
##
## background test p-values:
## D I rank.cor env.D env.I env.cor
## 0.2 0.2 0.2 0.2 0.2 0.2
##
##
## Replicates:
##
##
## D I rank.cor env.D env.I env.cor
## ---------- ---------- ---------- ---------- ---------- ---------- ----------
## empirical 0.4929334 0.7052122 0.2916150 0.1087717 0.3135923 0.2259397
## rep 1 0.8906250 0.9776752 0.4662984 0.1576325 0.3815030 0.3645233
## rep 2 0.9821483 0.9991113 0.9021410 0.6677139 0.8398918 0.7264567
## rep 3 0.8152793 0.9423609 0.3708291 0.1281372 0.3422291 0.3259818
## rep 4 0.8317838 0.9455251 0.5283673 0.1433208 0.3607374 0.3343933
ENMTools also allows you to perform linear, blob, and ribbon rangebreak tests as developed in Glor and Warren 2011. The linear and blob tests are two versions of a test that permit one to ask whether the geographic regions occupied by two species are more environmentally different than expected by chance. The ribbon test, meanwhile, is designed to test whether the ranges of two species are divided by a region that is relatively unsuitable to one or both forms.
For the linear and blob tests, you call them very much like you would the identity and background tests. Here’s a linear one using GLM models:
rbl.glm <- rangebreak.linear(ahli, allogus, env, type = "glm", nreps = 4)
##
## Building empirical models...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
##
## Building replicate models...
##
## Replicate 1 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
##
## Replicate 2 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
##
## Replicate 3 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
##
## Replicate 4 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
rbl.glm
##
##
##
##
## Linear rangebreak test ahli vs. allogus
##
## rangebreak test p-values:
## D I rank.cor env.D env.I env.cor
## 0.8 0.8 0.4 0.2 0.2 0.4
##
##
## Replicates:
##
##
## D I rank.cor env.D env.I env.cor
## ---------- ---------- ---------- ----------- ---------- ---------- -----------
## empirical 0.2370142 0.4876761 -0.4777383 0.0048679 0.0306320 -0.6090823
## rep 1 0.0594941 0.2149724 0.7888570 0.6504261 0.8280126 0.7867806
## rep 2 0.2542324 0.5641105 -0.9030459 0.0109390 0.0474069 -0.8224714
## rep 3 0.1576529 0.3739131 0.7504090 0.6497932 0.8416160 0.7844077
## rep 4 0.0690815 0.2344068 0.6448373 0.5622205 0.7808174 0.5862241
And here’s a blob test using Bioclim:
rbb.bc <- rangebreak.blob(ahli, allogus, env, type = "bc", nreps = 4)
##
## Building empirical models...
##
## Building replicate models...
##
## Replicate 1 ...
##
## Replicate 2 ...
##
## Replicate 3 ...
##
## Replicate 4 ...
rbb.bc
##
##
##
##
## blob rangebreak test ahli vs. allogus
##
## rangebreak test p-values:
## D I rank.cor env.D env.I env.cor
## 1.0 0.8 0.8 0.4 0.4 0.4
##
##
## Replicates:
##
##
## D I rank.cor env.D env.I env.cor
## ---------- ---------- ---------- ----------- ---------- ---------- ----------
## empirical 0.1328502 0.3177390 0.0706201 0.0245870 0.1187610 0.0915971
## rep 1 0.1328502 0.3177390 0.0706201 0.0184350 0.1023159 0.0799786
## rep 2 0.0387796 0.1696376 0.0010126 0.2486281 0.3452524 0.2872334
## rep 3 0.1315789 0.3269104 0.1649528 0.2056685 0.4274906 0.3478539
## rep 4 0.0186079 0.1114632 -0.0648470 0.1760290 0.2456170 0.1952171
If you want to access the individual replicates (for instance to see how your ranges are being split up), you can find them in the list named “replicate.models” inside your rangebreak test object.
rbl.glm$replicate.models$ahli.rep.1
##
##
## Formula: presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7fb54aedaa78>
##
##
## Data table (top ten lines):
##
## Longitude Latitude layer.1 layer.2 layer.3 layer.4 presence
## --- ---------- --------- -------- -------- -------- -------- ---------
## 81 -74.1691 20.2170 1608 1722 972 445 1
## 79 -74.2483 20.2963 1935 1503 923 521 1
## 80 -74.2351 20.2302 1932 1423 998 485 1
## 78 -74.2483 20.2434 1933 1431 954 485 1
## 77 -74.2628 20.2134 1987 1336 1027 474 1
## 75 -74.3143 20.2434 2064 1330 901 528 1
## 76 -74.2747 20.1114 1972 1243 1112 422 1
## 73 -74.4331 20.2963 2690 1120 385 775 1
## 74 -74.3803 20.1378 2060 1291 918 503 1
## 72 -74.4595 20.1906 2190 1196 994 502 1
##
##
## Model:
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1,
## 2)])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.08580 -0.00006 0.00000 0.00000 2.69248
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 52.930079 14.016240 3.776 0.000159 ***
## layer.1 -0.027479 0.008570 -3.206 0.001345 **
## layer.2 -0.022780 0.007093 -3.212 0.001320 **
## layer.3 0.004083 0.006174 0.661 0.508344
## layer.4 0.057365 0.025629 2.238 0.025201 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 186.634 on 2015 degrees of freedom
## Residual deviance: 81.715 on 2011 degrees of freedom
## AIC: 91.715
##
## Number of Fisher Scoring iterations: 14
##
##
##
## Model fit (training data): class : ModelEvaluation
## n presences : 16
## n absences : 2000
## AUC : 0.9875
## cor : 0.2192967
## max TPR+TNR at : -3.597793
##
##
## Proportion of data wittheld for model fitting: 0
##
## Model fit (test data): [1] NA
##
##
## Suitability:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer
## values : 2.220446e-16, 0.5752632 (min, max)
rbl.glm$replicate.models$allogus.rep.1
##
##
## Formula: presence ~ layer.1 + layer.2 + layer.3 + layer.4
## <environment: 0x7fb550caff90>
##
##
## Data table (top ten lines):
##
## Longitude Latitude layer.1 layer.2 layer.3 layer.4 presence
## --- ---------- --------- -------- -------- -------- -------- ---------
## 63 -74.8293 20.5471 2418 1234 903 604 1
## 65 -74.8029 20.3359 2268 1102 1199 419 1
## 61 -74.9481 20.6264 2345 1410 738 656 1
## 62 -74.9349 20.5339 2312 1107 1243 427 1
## 59 -74.9877 20.6528 2328 1492 779 664 1
## 66 -74.7897 20.1510 2178 1302 975 464 1
## 58 -75.0141 20.6132 2296 1484 791 641 1
## 60 -74.9745 20.1114 2018 1467 1012 355 1
## 57 -75.0273 20.1510 2022 1563 972 330 1
## 54 -75.2782 20.6396 2089 1642 871 617 1
##
##
## Model:
## Call:
## glm(formula = f, family = "binomial", data = analysis.df[, -c(1,
## 2)])
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.3236 -0.2258 -0.1944 -0.1805 2.9623
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -1.377e-01 2.421e+00 -0.057 0.9546
## layer.1 -7.969e-04 5.471e-04 -1.457 0.1452
## layer.2 -1.093e-03 5.204e-04 -2.100 0.0358 *
## layer.3 -1.941e-04 1.118e-03 -0.174 0.8621
## layer.4 2.674e-05 1.285e-03 0.021 0.9834
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 629.56 on 3064 degrees of freedom
## Residual deviance: 623.74 on 3060 degrees of freedom
## AIC: 633.74
##
## Number of Fisher Scoring iterations: 7
##
##
##
## Model fit (training data): class : ModelEvaluation
## n presences : 65
## n absences : 3000
## AUC : 0.6021436
## cor : 0.04424549
## max TPR+TNR at : -3.923229
##
##
## Proportion of data wittheld for model fitting: 0
##
## Model fit (test data): [1] NA
##
##
## Suitability:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer
## values : 0.009675917, 0.05688295 (min, max)
For the ribbon rangebreak test, you will need one extra thing; a third enmtools.species object representing the occurrence points (for one or both species) that fall within the ribbon of putatively unsuitable habitat. In the case of these two anoles we don’t have such a ribbon, so we’ll just simulate one based on some random points.
ribbon <- enmtools.species(species.name = "ribbon")
ribbon$presence.points <- data.frame(Longitude = runif(n = 10, min = -79, max = -78.5),
Latitude = runif(n = 10, min = 21.7, max = 22.1))
plot(env[[1]])
points(ribbon$presence.points, pch = 16)
ribbon$range <- background.raster.buffer(ribbon$presence.points, 20000, mask = env)
ribbon
##
##
## Range raster:
## class : RasterLayer
## dimensions : 418, 1535, 641630 (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333 (x, y)
## extent : -86.90809, -74.11642, 19.80837, 23.2917 (xmin, xmax, ymin, ymax)
## coord. ref. : NA
## data source : in memory
## names : layer.1
## values : 1, 1 (min, max)
##
##
##
## Presence points (first ten only):
##
## Longitude Latitude
## ---------- ---------
## -78.75728 21.78110
## -78.54600 22.08576
## -78.87652 21.94261
## -78.65133 21.83508
## -78.84400 21.89280
## -78.97722 21.78778
## -78.72197 21.85385
## -78.65541 21.74081
## -78.56190 22.05039
## -78.79772 21.83139
##
##
## Background points not defined.
##
## Species name: ribbon
Now we’ll run a ribbon rangebreak test using GLM models with quadratic effects. We also need to tell it the width of the ribbons to generate for the replicates. The units for the width argument are the same units that the presence points are in; e.g., if the points are in decimal degrees you should supply the width of the barrier in decimal degrees.
rbr.glm <- rangebreak.ribbon(ahli, allogus, ribbon, env, type = "glm", f = pres ~ poly(layer.1, 2) + poly(layer.2, 2) + poly(layer.3, 2) + poly(layer.4, 2), width = 0.5, nreps = 4)
##
##
## No background points provided, drawing background from range raster.
## Warning in couldBeLonLat(mask): CRS is NA. Assuming it is longitude/
## latitude
##
## Building empirical models...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
## Adding environmental data to species ribbon
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside
## Processing presence points...
## Processing background points...
##
## Building replicate models...
##
## Replicate 1 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
## Adding environmental data to species ribbon
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside
## Processing presence points...
## Processing background points...
##
## Replicate 2 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
## Adding environmental data to species ribbon
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside
## Processing presence points...
## Processing background points...
##
## Replicate 3 ...
##
## Replicate 3 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
## Adding environmental data to species ribbon
## Processing presence points...
## Processing background points...
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside
## Processing presence points...
## Processing background points...
##
## Replicate 4 ...
## Adding environmental data to species ahli
## Processing presence points...
## Processing background points...
## Adding environmental data to species allogus
## Processing presence points...
## Processing background points...
## Adding environmental data to species ribbon
## Processing presence points...
## Processing background points...
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
## Adding environmental data to species outside
## Processing presence points...
## Processing background points...
rbr.glm
##
##
##
##
## ribbon rangebreak test ahli vs. allogus
##
## rangebreak test p-values...
##
## Species 1 vs. Species 2:
## D I rank.cor env.D env.I env.cor
## 0.6 0.6 0.6 0.4 0.2 0.6
##
## Species 1 vs. Ribbon:
## D I rank.cor env.D env.I env.cor
## 0.8 0.8 0.2 0.4 0.2 0.8
##
## Species 2 vs. Ribbon:
## D I rank.cor env.D env.I env.cor
## 0.4 0.4 0.2 0.2 0.2 0.6
##
## Outside vs. Ribbon:
## D I rank.cor env.D env.I env.cor
## 0.4 0.4 0.2 0.2 0.2 0.2
##
##
## Replicates:
##
## Species 1 vs. Species 2:
## D I rank.cor env.D env.I env.cor
## empirical 0.26549786 0.4949346 0.1215859 0.01460094 0.05664139 -0.3146633
## rep 1 0.05682307 0.2066092 0.1986456 0.09809980 0.22467871 -0.2208879
## rep 2 0.09383292 0.2993899 0.7303031 0.40201752 0.65496386 0.4560309
## rep 3 0.44239443 0.7248231 -0.1955811 0.08605595 0.21739904 -0.3424740
## rep 4 0.35060254 0.6217386 -0.1501862 0.01228363 0.05964101 -0.6333295
##
## Species 1 vs. Ribbon:
## D I rank.cor env.D env.I
## empirical 0.190342635 0.37176871 0.03391003 0.0015179924 0.004199239
## rep 1 0.016939599 0.06944241 0.45564427 0.0114945984 0.041325320
## rep 2 0.005526904 0.03760518 0.20620997 0.0407164789 0.100495847
## rep 3 0.028850143 0.11955792 0.19023240 0.0036378953 0.021311169
## rep 4 0.362392781 0.64145321 0.24518570 0.0007379238 0.004452770
## env.cor
## empirical 0.007320921
## rep 1 -0.162525924
## rep 2 -0.169379344
## rep 3 -0.336462391
## rep 4 0.073991468
##
## Species 2 vs. Ribbon:
## D I rank.cor env.D env.I
## empirical 0.05248188 0.1839138 -0.79035255 0.00153894 0.02167625
## rep 1 0.44706159 0.7321735 -0.07050705 0.04044356 0.14712351
## rep 2 0.23060629 0.4814952 0.21608878 0.02444426 0.11882495
## rep 3 0.03326870 0.1419511 0.09692508 0.09556927 0.25062191
## rep 4 0.51379889 0.7692721 0.43397767 0.08071482 0.23373031
## env.cor
## empirical -0.163480088
## rep 1 0.433225364
## rep 2 -0.181451910
## rep 3 0.009139382
## rep 4 -0.268965244
##
## Outside vs. Ribbon:
## D I rank.cor env.D env.I
## empirical 0.06932105 0.2188043 -0.83674808 0.001450172 0.02246389
## rep 1 0.43447295 0.7089559 0.03309588 0.024286708 0.10269149
## rep 2 0.19924394 0.4415494 0.09792724 0.118677259 0.32353251
## rep 3 0.03228930 0.1385082 -0.02561359 0.169444819 0.37147638
## rep 4 0.51704994 0.7793329 0.31281568 0.136662782 0.32948549
## env.cor
## empirical -0.21311107
## rep 1 0.06448792
## rep 2 0.06130434
## rep 3 0.12938080
## rep 4 -0.20345529
Note that the output table here has slope, intercept, and intercept offset.
rbr.glm$lines.df
## slope intercept offset
## 1 -0.6297962 -27.32158 0.2954492
## 2 40.7177016 3144.41695 10.1824949
## 3 1.4330286 132.12021 0.4368617
## 4 -1.4263084 -89.48284 0.4354851
The intercept denotes the intercept corresponding to the CENTER of each ribbon. To get the lines denoting the edges of the ribbons (for example if you want to plot the ribbons on a map), you add and substract the offset. In other words, the top edge of the ribbon is given by y = (slope * x) + intercept + offset, while the bottom edge is given by y = (slope * x) + intercept - offset.
Levins, R. 1968. Evolution In Changing Environments. Monographs in Population Biology, volume 2. Princeton University Press, Princeton, New Jersey, USA.
Schoener, T. W. 1968. Anolis lizards of Bimini: resource partitioning in a complex fauna. Ecology 49:704- 726.
Warren, D.L., R.E. Glor, and M. Turelli. 2008. Environmental niche identity versus conservatism: quantitative approaches to niche evolution. Evolution 62:2868-2883. doi: 10.1111/j.1558-5646.2008.00482.x
Warren, D.L., M. Cardillo, D.F. Rosauer, and D.I. Bolnick. 2014. Mistaking geography for biology: inferring processes from species distributions. Trends in Ecology and Evolution 29 (10), 572-580. doi: 10.1016/j.tree.2014.08.003